var newsPublish = new XFApp.client();
var ue = UE.getEditor('container', {
	toolbars : toolbars
});

XFApp.client.prototype.newsPublish = {
	successCode : "10000",

	formValidate : function(){
		
		$("#editForm").validate({
			rules: {
				title: {
					required: true
				},
				author: {
					required: true
				},
				source: {
					required: true
				}
			},
			messages: {
				title: {
					required: "请输入标题"
				},
				author: {
					required: "请输入作者"
				},
				source: {
					required: "请输入文章来源"
				}
			}
		});
		
	},
	
	submitNews : function(type) {
		var params = {
			content : ue.getContent(),
		};
		$("input").each(function() {
			if(!!$(this).attr("name")){
				if($(this).attr("type")=="radio"){
					if(!!$(this).prop("checked")){
						params[$(this).attr("name")] = $(this).val();
					}
				}else{
					params[$(this).attr("name")] = $(this).val();
				}
			}
		});
		$("select").each(function() {
			if(!!$(this).attr("name"))
				params[$(this).attr("name")] = $(this).val();
		});
		$("textarea").each(function(){
			if(!!$(this).attr("name"))
				params[$(this).attr("name")] = $(this).val();
		});
		if(type==2){
			params["id"]=null;
		}
		webDialogUtil.simpleConfrimAlert("是否确认提交", function(){
			$.ajax({
				url : basePath + "/article/save",
				data : params,
				dataType : 'json',
				type : 'post',
				success : function(data) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}else{
						webDialogUtil.simpleAlert("处理成功", "温馨提示");
						if(!!data.result){
							$("input[name='id']").val(data.result.id);
							$("#mod_btn").show();
							}
						}
					}
			});
		}, null, "温馨提示");
		
	},
	event : function() {
		_this = this;
		_this.formValidate();
		$("#submit_btn").on("click",function(){
			if($("#editForm").validate().form()){
				_this.submitNews(2);
			}
		});
		$("#mod_btn").on("click",function(){
			if($("#editForm").validate().form()){
				_this.submitNews(1);
			}
		});
	},
};
$(function() {
	if(!!$("input[name='id']").val()){
		$("#mod_btn").show();
		$("#submit_btn").hide();
	}
	newsPublish.newsPublish.event();
	ue.addListener("ready", function () {
        // editor准备好之后才可以使用
		if(!!$("#edit_content").html()){
			 ue.setContent($("#edit_content").html());
		}
    });
});